/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-28     17247       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>

#define UART1_NAME       "uart1"

/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem1;
static rt_device_t serial1;

/* 接收数据回调函数 */
static rt_err_t uart1_input(rt_device_t dev, rt_size_t size)
{
    rt_sem_release(&rx_sem1);
    return RT_EOK;
}

/* 接收线程入口函数 */
static void uart1_thread_entry(void *parameter)
{
    char ch;

    while (1)
    {
        /* 从串口读取一个字节 */
        while (rt_device_read(serial1, -1, &ch, 1) != 1)
        {
            rt_sem_take(&rx_sem1, RT_WAITING_FOREVER);
        }

        /* 将接收到的数据回显 */
        rt_device_write(serial1, 0, &ch, 1);
    }
}

/**
 * @brief UART1 初始化函数
 *
 * @return int RT_EOK(0) 表示成功，其他值表示错误
 */
static int uart1_init(void)
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];
    char str[] = "UART1 initialized!\r\n";

    // 使用默认串口名称
    rt_strncpy(uart_name, UART1_NAME, RT_NAME_MAX);

    /* 查找串口设备 */
    serial1 = rt_device_find(uart_name);
    if (!serial1)
    {
        rt_kprintf("[UART1] Device %s not found!\n", uart_name);
        return -RT_ERROR;
    }

    /* 初始化信号量 */
    rt_sem_init(&rx_sem1, "rx1_sem", 0, RT_IPC_FLAG_FIFO);

    /* 打开设备 */
    if (rt_device_open(serial1, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
    {
        rt_kprintf("[UART1] Open device failed!\n");
        return -RT_ERROR;
    }

    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial1, uart1_input);

    /* 发送初始化消息 */
    rt_device_write(serial1, 0, str, rt_strlen(str));
    rt_kprintf("[UART1] Initialized and test message sent\n");

    /* 创建接收线程 */
    rt_thread_t thread = rt_thread_create("uart1_rx",
                                         uart1_thread_entry,
                                         RT_NULL,
                                         1024,
                                         25,
                                         10);

    /* 启动线程 */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        rt_kprintf("[UART1] Thread create failed!\n");
        ret = -RT_ERROR;
    }

    return ret;
}

/* 使用设备级初始化 */
INIT_DEVICE_EXPORT(uart1_init);
